/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Alexandru.Codescu
 */
@Entity
@Table(name = "absente")
@NamedQueries({
    @NamedQuery(
        name = "Absenta.queryBySpatiu",
        query = "select count(a.id), a.firma.id  "
                + "from Absenta a "
                + "where a.spatiuComercial.id = ?1 "
                + "and a.firma.id = ?2 "
                + "and dataAbsenta >= ?3"),
    @NamedQuery(
            name = "Absenta.queryByDay",
            query = "select a.spatiuComercial.cod, a.firma.denumire, a.spatiuComercial.id, a.dataAbsenta, a.id, sum(c.valoare), count(a.spatiuComercial.cod) "
                    + "from Absenta a "
                    + "left join a.spatiuComercial sc "
                    + "left join sc.inchiriere inc "
//                    + "with (inc.dataInceput >= ?1) and (inc.dataInceput < ?2) "
                    + "with DATE(inc.dataInceput) = ?1 "
                    + "left join inc.creanta c with (c.taxa.id in (1,4,5)) "
                    + "where a.dataAbsenta = ?1 "
                    + "group by a.spatiuComercial.id"),
                    
    @NamedQuery(
            name = "Absenta.queryByDayAll",
            query = "select a.spatiuComercial.cod, a.firma.denumire, a.spatiuComercial.id, a.dataAbsenta, a.id "
                    + "from Absenta a "
                    + "left join a.spatiuComercial sc "
                    + "where a.dataAbsenta = ?1 "),
                    
    @NamedQuery(
            name = "Absenta.queryByTaraba",
            query = "select a.spatiuComercial.cod, a.firma.denumire, a.spatiuComercial.id, a.dataAbsenta, a.id "
                    + "from Absenta a "
                    + "where a.spatiuComercial.id = ?1 " +
                    "order by a.dataAbsenta desc"),
    @NamedQuery(
            name = "Absenta.queryByDayTotal",
            query = "select count(a.id)  "
                    + "from Absenta a "
                    + "where dataAbsenta = ?1")
    
})
public class Absenta extends AbstractEntitate {
    private SpatiuComercial spatiuComercial;
    private Firma firma;
    private Date dataAbsenta;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_SPATIU_COMERCIAL")
    public SpatiuComercial getSpatiuComercial() {
        return spatiuComercial;
    }

    public void setSpatiuComercial(SpatiuComercial spatiuComercial) {
        this.spatiuComercial = spatiuComercial;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_FIRMA")
    public Firma getFirma() {
        return firma;
    }

    public void setFirma(Firma firma) {
        this.firma = firma;
    }

    @Column(name = "DATA_ABSENTA")
    @Temporal(TemporalType.DATE)
    public Date getDataAbsenta() {
        return dataAbsenta;
    }

    public void setDataAbsenta(Date dataAbsenta) {
        this.dataAbsenta = dataAbsenta;
    }
}
